/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mcarthur.countdown;

import java.nio.ByteBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;

class CountdownRenderer implements GLSurfaceView.Renderer {
	public static final float WIDTH_RATIO = 1.25f;
	
	public static float viewWidth;
	public static float viewHeight;
	
	private CountdownClock mClock;
	private Context context;
    
    public CountdownRenderer(Context context) {
    	this.context = context;
    	float posX = (CountdownClock.WIDTH * (WIDTH_RATIO - 1)) / 2.0f;
        mClock = new CountdownClock(posX);
    }

    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        mClock.draw(gl);
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
         gl.glViewport(0, 0, width, height);

         /*
          * Set our projection matrix. This doesn't have to be done
          * each time we draw, but usually a new projection needs to
          * be set when the viewport is resized.
          */

         float ratio = (float) height / width;
         gl.glMatrixMode(GL10.GL_PROJECTION);
         gl.glLoadIdentity();
         viewWidth = CountdownClock.WIDTH * WIDTH_RATIO;
         viewHeight = viewWidth * ratio;
         gl.glOrthof(0, viewWidth, 0, viewHeight, -1, 1);
         
         mClock.setPosY((viewHeight - CountdownClock.HEIGHT) / 2.0f);
         
         // COUNTDOWN IS 930 x 128
    }

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		gl.glDisable(GL10.GL_DITHER);
		gl.glDisable(GL10.GL_CULL_FACE);
		gl.glDisable(GL10.GL_DEPTH_TEST);

		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

		gl.glClearColor(1, 1, 1, 1);

		loadTexture(gl);
	}

	// Load/configure the ball texture.
	public void loadTexture(GL10 gl) { 
		int texture[] = new int[1];

		gl.glGenTextures(1, texture, 0);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);

		byte[] texCoords = { 0, 0, 0, 1, 1, 0, 1, 1 };
		ByteBuffer texcoords = ByteBuffer.allocateDirect(texCoords.length);
        texcoords.put(texCoords);
        texcoords.position(0);
		
        gl.glTexCoordPointer(2, GL10.GL_BYTE, 0, texcoords);
		Bitmap ball = BitmapFactory.decodeResource(context.getResources(), R.drawable.ball);

		//Setup optional texture parameters
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

		//Set the texture image
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, ball, 0); 

		//Enable texture related flags
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

		// Transparency
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
	}
}
